//  Inside Microsoft Exchange 2007 Web Services 
//  Chapter 17
//
//  Copyright (c) 2007 David Sterling, Ben Spain, Mark Taylor, Huw Upshall, Michael Mainer.  
//  All rights reserved.
//
using System;
using System.Collections.Generic;
using System.Text;
using System.Net;
using System.IO;
using ProxyHelpers.EWS;

namespace CallingUnsubscribe
{
    class Program
    {
        static void Main(string[] args)
        {
            ExchangeServiceBinding binding = new ExchangeServiceBinding();
            binding.Url = @"https://yourServer/ews/Exchange.asmx";
            binding.Credentials = new NetworkCredential("username", "password", "domain");

            // TODO:  You must have a valid subscription id in order to call Unsubscribe below.
            //
            // Unsubscribe(binding, yourSubscriptionId);
        }

        /// <summary>
        /// Unsubscribe from a Pull Notification Subscription (Listing 17-14)
        /// </summary>
        /// <param name="binding">EWS binding</param>
        /// <param name="subscriptionId">Subscription ID</param>
        private static void Unsubscribe(ExchangeServiceBinding binding,
                                        string subscriptionId)
        {
            // Call the overloaded constructor to create the Unsubscribe request
            UnsubscribeType unsubscribeRequest = new UnsubscribeType(subscriptionId);

            // Call the Unsubscribe method and check that the call succeeds
            UnsubscribeResponseType unsubscribeResponse =
                binding.Unsubscribe(unsubscribeRequest);
            if (unsubscribeResponse.ResponseMessages.Items[0].ResponseCode !=
                ResponseCodeType.NoError)
            {
                throw new Exception("Unsubscribe failed for subscription: " +
                    subscriptionId);
            }
        }
    }
}
